<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div class="ml-2"><el-button size="small" type="warning" @click="addUrl" icon="el-icon-plus">添加地址</el-button>
      </div>
    </div>
    <div class="d-f">
      <div class="flex-1 ml-2">
        <el-table :data="siteguideList" border stripe>
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column align="center" label="导航名称" prop="title" width="90" />
          <el-table-column label="地址" prop="linkurl" width="160" />
          <el-table-column align="center" label="导航注解" prop="sub_title" width="45" />
          <el-table-column align="center" label="操作" width="130">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="导航">
      <el-form :model="formSiteguide" ref="form" label-width="80px">
        <el-form-item label="导航名称" prop="title">
          <div style="width: 40%;">
            <el-input v-model="formSiteguide.title"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="导航注解" prop="sub_title">
          <div style="width: 60%;">
            <el-input v-model="formSiteguide.sub_title"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFormUrl">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    siteguideListUrl,
    siteguideCreateUrl,
    siteguideDetailUrl,
    siteguideUpdateUrl,
    siteguideDeleteUrl,
  } from "@/utils/clientistration"
  export default {
    name: 'brand',
    data() {
      return {
        dialogVisible: false,
        queryInfo: {
          page: 1
        },
        siteguideList: [],
        formSiteguide: {
          image: '',
          title: '',
          sub_title: '',
          sort: '',
          routeurl: ''
        },
        addUrlStatu: true,
        getUrlId: ''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getSiteguideList()
      },
      getSiteguideList() {
        this.$get(siteguideListUrl, this.queryInfo).then(res => {
          this.siteguideList = res.data.data.list
          this.routeurTotal = res.data.data.total
        })
      },
      addUrl() {
        this.dialogVisible = true
        this.addUrlStatu = true
        this.formSiteguide = {}
      },
      submitFormUrl() {
        if (this.addUrlStatu) {
          this.$post(routeurlCreateUrl, this.formSiteguide).then(res => {
              this.getRouteurlList()
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.formSiteguide = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        } else {
          this.$put(routeurlUpdateUrl + this.getUrlId + '/', this.formSiteguide).then(res => {
              this.getRouteurlList()
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.formSiteguide = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        }
      },
      openDelete(urlId) {
        this.$confirm('此操作将永久删除该路由, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(routeurlDeleteUrl + urlId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getRouteurlList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleEdit(siteguideId) {
        this.addUrlStatu = false
        this.dialogVisible = true
        this.getUrlId = urlId
        this.$get(siteguideDetailUrl + siteguideId + '/').then(res => {
          if (res.data.code == 200) {
            console.log(res.data.data);
            this.formSiteguide = {
              image: res.data.data.image,
              title: res.data.data.title,
              sub_title: res.data.data.sub_title,
              sort: res.data.data.sort,
              routeurl: res.data.data.routeurl
            }
          }
        })
      }
    }
  }
</script>
<style lang="scss">

</style>